package com.xique.door.biz.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xique.common.core.constant.Constants;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.WxMessageTemplate;
import com.xique.door.bean.request.wxMessage.WxMessageTemplateAddRequest;
import com.xique.door.bean.request.wxMessage.WxMessageTemplateEditRequest;
import com.xique.door.bean.request.wxMessage.WxMessageTemplateListRequest;
import com.xique.door.bean.search.WxMessageTemplateListSearch;
import com.xique.door.bean.vo.wxMessage.WxMessageTemplateDetailVO;
import com.xique.door.bean.vo.wxMessage.WxMessageTemplateListVO;
import com.xique.door.biz.service.IWxMessageTemplateBizService;
import com.xique.door.service.IWxMessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shd
 * @create 2022/4/27 13:11
 */
@Service
public class WxMessageTemplateBizServiceImpl implements IWxMessageTemplateBizService {

    @Autowired
    private IWxMessageTemplateService messageTemplateService;

    @Override
    public AjaxResult getTemplateList(WxMessageTemplateListRequest request) {

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", new ArrayList<>());
        map.put("total", 0);

        WxMessageTemplateListSearch search = OrikaUtil.convert(request, WxMessageTemplateListSearch.class);
        IPage<WxMessageTemplate> page = messageTemplateService.selectTempLatePage(search);
        List<WxMessageTemplate> list = page.getRecords();
        if (CollectionUtil.isEmpty(list)) {
            return AjaxResult.success(map);
        }

        List<WxMessageTemplateListVO> vos = OrikaUtil.converts(list, WxMessageTemplateListVO.class);

        map.put("list", vos);
        map.put("total", page.getTotal());

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getTemplateDetail(Long id) {

        if (StringUtils.isNull(id)) {
            return AjaxResult.error("请传入模板id");
        }

        WxMessageTemplate template = messageTemplateService.getById(id);
        if (StringUtils.isNull(template)) {
            return AjaxResult.error("未找到对应的模板");
        }

        WxMessageTemplateDetailVO vo = OrikaUtil.convert(template, WxMessageTemplateDetailVO.class);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult addTemplate(WxMessageTemplateAddRequest request) {

        int count = messageTemplateService.checkTemplateUnique(request.getType());
        if (count > 0) {
            return AjaxResult.error("该类型模板已存在，请先停用已存在，再添加");
        }

        WxMessageTemplate template = OrikaUtil.convert(request, WxMessageTemplate.class);
        template.setCreateUser(SecurityUtils.getUserId());

        boolean isSuccess = messageTemplateService.save(template);
        if (!isSuccess) {
            return AjaxResult.error("添加失败");
        }

        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult changeStatus(WxMessageTemplateEditRequest request) {

        if (Constants.NORMAL.equals(request.getStatus())) {
            WxMessageTemplate template = messageTemplateService.getById(request.getId());
            int count = messageTemplateService.checkTemplateUnique(template.getType());
            if (count > 0) {
                return AjaxResult.error("该类型模板已存在，请先停用已存在，再启用新的");
            }
        }
        WxMessageTemplate update = new WxMessageTemplate();
        update.setId(request.getId());
        update.setStatus(request.getStatus());
        update.setUpdateUser(SecurityUtils.getUserId());

        boolean isSuccess = messageTemplateService.updateById(update);
        if (!isSuccess) {
            return AjaxResult.error("修改失败");
        }

        return AjaxResult.success("修改成功");
    }
}
